(function(){
_PATHINFO = ownPath() ;

D(function(){
	if(!$("."+dcc).length)return ;
	dcInit({
		showMaxWidth : typeof showMaxWidth != "undefined" ? showMaxWidth : 980 ,
		showMaxHeight : typeof showMaxHeight != "undefined" ? showMaxHeight : 650 ,
		thumbMaxWidth : 117 ,
		thumbMaxHeight : 87 ,
		ulMaskWidth : typeof showMaxWidth != "undefined" ? showMaxWidth - 70 : 910
	},function(c){
		dcElemCurr = $(".sc_curr") ;
		// 更新激活索引
		currIndex = c.index ;
		dcElemCurr.text(c.index + 1) ;
	},function(c){
		dcElemCount = $(".sc_count") ;
		// 赋值图片总数
		dcElemCount.text(c.count) ;
	}) ;
}) ;


var dcElemCurr ,														// .sc_curr
	dcElemCount ,														// .sc_count
	dcc = "drunk_carousel" ,											// main class
	dc ,																// .drunk_carousel
	dcShow ,															// .drunk_carousel .show
	dcAlt ,																// .drunk_carousel .alt
	dcListWarp ,														// .drunk_carousel .list
	dcListMask ,														// .drunk_carousel .list .ulMask
	dcList ,															// .drunk_carousel .list ul
	dcListElem ,														// .drunk_carousel .list ul li
	dcShowImg ,															// .drunk_carousel .show img
	dcListPre ,															// .drunk_carousel .list .pre
	dcListNext ,														// .drunk_carousel .list .next
	did = String(location.hash.match(/\d+$/) || 0)*1 ,					// #id=
	currIndex = 0 ,
	
	maskWidth ,
	liWidth ,
	liCount ,
	ulWidth ,
	hiddenLiCount ;
	
	
var dcInit = function(con, switchCall, call){
	dcCss(con.ulMaskWidth) ;
	dc = $("."+ dcc) ;
	dcShow = dc.children(".show") ;
	dcAlt = dc.children(".alt") ;
	dcListWarp = dc.children(".list") ;
	dcListMask = dcListWarp.children(".ulMask") ;
	dcList = dcListMask.children("ul") ;
	dcListElem =  dcList.children("li") ;
	dcListPre = dcListWarp.children(".pre") ;
	dcListNext = dcListWarp.children(".next") ;
	
	maskWidth = dcListMask.innerWidth() ;
	liWidth = $(dcListElem[0]).outerWidth(true) ;
	liCount = dcListElem.length ;
	ulWidth = liWidth * liCount ;
	hiddenLiCount = Math.floor((ulWidth - maskWidth) / liWidth) ;
	
	var dcPreImg = $(".dcPreImg") ,
		dcNextImg = $(".dcNextImg") ,
		maskLeft = dcListMask.offset().left ,
		liLeft ;
	
	// 
	dc[0].onselectstart = function(){return false;} ;
	
	// 重设ul宽度
	dcList.width(ulWidth);
	
	// 绑定向前按钮的点击事件
	dcListPre.on("click", function(){scrollR() ;}) ;
	
	// 绑定向后按钮的点击事件
	dcListNext.on("click", function(){scrollL() ;}) ;
	
	// 绑定图片切换事件
	dcPreImg.on("click", function(){
		if(currIndex <= 0)return alert("已经浏览到第一张了!") ;
		$(dcListElem[currIndex - 1]).trigger("click") ;
	}) ;
	
	dcNextImg.on("click", function(){
		if(currIndex >= liCount - 1)return alert("已经浏览到最后一张了!") ;
		$(dcListElem[currIndex + 1]).trigger("click") ;
	}) ;
	
	// 列表相关绑定
	dcListElem.each(function(index, element) {
        var elem = $(element) ,
			image = elem.find("img") ;
		
		element.index = index ;
		element.elem = elem ;
		element.alt = image.attr("alt") ;
		element.img = image.attr("data-img") ;
		element.did = image.attr("data-id").match(/\d+$/) ;
		
		elem.on("click", function(){
			// 滚左
			liLeft = this.elem.offset().left ;
			if(liLeft < maskLeft){
				dcListPre.trigger("click") ;
			// 滚右
			}else if(liLeft > maskLeft + maskWidth){
				dcListNext.trigger("click") ;
			}

			dcShowImg = $("<img>", {src : this.img}).appendTo(dcShow.empty()) ;
			dcPreImg.html('<img src="'+this.img+'" />');
			dcNextImg.html('<img src="'+this.img+'" />');
			
			// 加载图片描述
			dcAlt.html(this.alt) ;
			
			// 切换列表激活状态
			DC.tabActive(this, dcListElem, 0, "hover") ;
			
			// 调整图片尺寸
			DC.photo(dcShowImg, con.showMaxWidth, con.showMaxHeight)
			
			// 记录当前图片
			location.hash = "id=" + this.did ;
			
			// 回调
			if(typeof switchCall == "function")switchCall({
				index : this.index
			}) ;
		}) ;
		
		// 调整略缩图尺寸
		DC.photo(image, con.thumbMaxWidth, con.thumbMaxHeight) ;
		
		// 切换到初始显示图片
		if(did && did == String(image.attr("data-id").match(/\d+$/)) * 1){
			elem.trigger("click") ;
			currIndex = index ;
		}else if(!did && index === liCount - 1){
			$(dcListElem[0]).click() ;
		}
		
		// 回调
		if(typeof call == "function")call({
			count : liCount
		}) ;
    });
}

var b = 0, d = 100 , t=0 ;
var scrollL = function(){
	if(b >= liWidth * hiddenLiCount)return alert("已经滚动到最后!") ;
	dcListMask.scrollLeft(jQuery.easing.easeOutQuad(0,t,b,liWidth * 4,d));

	if(t<d){
		t++;
		window.setTimeout(scrollL, 1);
	}else{
		b += liWidth * 4 ;
		t = 0 ;
	}
}

var scrollR = function(){
	if(b <= 0)return alert("已经滚动到最前!") ;
	dcListMask.scrollLeft(jQuery.easing.easeOutQuad(0,t,b,-liWidth * 4,d));

	if(t<d){
		t++;
		window.setTimeout(scrollR, 1);
	}else{
		b -= liWidth * 4 ;
		t = 0 ;
	}
}

var dcCss = function(ulMaskWidth){
	$(document.body).append('<style type="text/css">' +
		'.suit_carousel{background:#0d0d0d;padding:8px;}' +
		'.suit_carousel_info{line-height:24px;color:#666;border-bottom:1px solid #666;margin-bottom:10px;}' +
		'.sc_title{font-size:18px;font-family:"微软雅黑";font-weight:bold;color:#fff;}' +
		'.sc_curr{color:#ff6702;font-size:16px;font-weight:bold;}' +
		'.sc_schedule a{color:#666;}' +
		
		'.dcPreImg,.dcNextImg{position:absolute;top:0;width:50%;height:650px;background-color:#fff\\9;filter:alpha(opacity=1)\\9;}'+
		'.dcPreImg{left:0;cursor:url('+_PATHINFO.dir+'control/left.cur),default;}' +
		'.dcNextImg{left:50%;cursor:url('+_PATHINFO.dir+'control/right.cur),default;}' +
		'.dcPreImg img,.dcNextImg img{width:100%;height:100%;opacity:0.01;filter:alpha(opacity=1)\9;}' +

		'.drunk_carousel{width:100%;overflow:hidden;position:relative;-moz-user-select:none;}' +
		'.drunk_carousel .show{height:650px;width:100%;overflow:hidden;}' +
		'.drunk_carousel .alt{width:100%;line-height:20px;color:#999;margin-top:10px;}' +
		
		'.drunk_carousel .list{width:100%;height:100px;padding:15px 0;overflow:hidden;margin-top:10px;border-top:1px solid #666;position:relative;}' +
		'.drunk_carousel .list div{float:left;width:20px;height:100%;cursor:pointer;background-repeat:no-repeat;background-position:center;}' +
		'.drunk_carousel .list div.pre{background-image:url('+_PATHINFO.dir+'control/goleft_click.gif);}' +
		'.drunk_carousel .list div.next{background-image:url('+_PATHINFO.dir+'control/goright_click.gif);}' +
		'.drunk_carousel .list div.ulMask{width:'+ulMaskWidth+'px;float:left;height:100%;margin:0 10px;overflow:hidden;}' +
		
		'.drunk_carousel .list ul li{float:left;width:117px;height:87px;padding:8px 3px 3px;overflow:hidden;margin-right:8px;}' +
		'.drunk_carousel .list ul li.hover{background-image:url('+_PATHINFO.dir+'control/thumb_img_bg.gif);}' +
		'.drunk_carousel .list ul li a{display:block;width:115px;height:85px;overflow:hidden;border:1px solid #666;background:#333;}' +
		'.drunk_carousel .list ul li.hover a{border-color:#fff;}' +
		'</style>'
	) ;
}
})() ;